package com.yudaob2b.elong.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.yudaob2b.business.entity.po.ElongHotelInventoryPO;
import com.yudaob2b.business.service.ElongHotelInventoryService;
import com.yudaob2b.elong.service.HotelInventoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * 新增/更新国内酒店-库存状态现场任务
 *
 * @author Kidd.Tan
 * @email 298023780@qq.com
 * @date 7/8/18 10:26 PM
 */
public class InsertOrUpdateInvTask implements Callable<String>, Serializable {
    private static Logger dataInvLogger = LoggerFactory.getLogger("dataInvLogger");

    private List<ElongHotelInventoryPO> invList;
    private ElongHotelInventoryService elongHotelInventoryService;
    private HotelInventoryService hotelInventoryService;

    public InsertOrUpdateInvTask(List<ElongHotelInventoryPO> invList, ElongHotelInventoryService elongHotelInventoryService, HotelInventoryService hotelInventoryService) {
        this.invList = invList;
        this.elongHotelInventoryService = elongHotelInventoryService;
        this.hotelInventoryService = hotelInventoryService;
    }

    @Override
    public String call() throws Exception {
        try {
            long start = System.currentTimeMillis();
            List<ElongHotelInventoryPO> newList = new ArrayList<>();
            List<ElongHotelInventoryPO> updateList = new ArrayList<>();
            EntityWrapper<ElongHotelInventoryPO> ew;
            for (ElongHotelInventoryPO elongHotelInventoryPO : invList) {
                // redis检查是否有数据落地标记
                int count = hotelInventoryService.hasCacheKey(elongHotelInventoryPO) == true ? 1 : 0;
                if(count == 0) {
                    // 数据库查询是否存在记录
                    ew = new EntityWrapper<>();
                    ew.where("hotel_id={0}", elongHotelInventoryPO.getHotelId())
                            .and("room_type_id={0}", elongHotelInventoryPO.getRoomTypeId())
                            .and("hotel_code={0}", elongHotelInventoryPO.getHotelCode())
                            .and("date={0}", elongHotelInventoryPO.getDate());
                    count = elongHotelInventoryService.selectCount(ew);
                }

                if (count > 0) {
                    // 已有记录更新操作
                    updateList.add(elongHotelInventoryPO);
                } else {
                    hotelInventoryService.cacheIndex(elongHotelInventoryPO);
                    // 没有记录添加操作
                    newList.add(elongHotelInventoryPO);
                }
            }
            dataInvLogger.info("--处理数据耗时--:" + (System.currentTimeMillis() - start));
            if (newList.size() > 0) {
                elongHotelInventoryService.insertBatch(newList);
            }
            dataInvLogger.info("--插入数据耗时--:" + (System.currentTimeMillis() - start));
            if (updateList.size() > 0) {
                elongHotelInventoryService.updateBatch(updateList);
            }
            dataInvLogger.info("--更新数据耗时--:" + (System.currentTimeMillis() - start));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ok";
    }

}
